<?
include("inc_security.php");

// Danh sách category truy cập
$classMenu	= new menu();
$listAll		= $classMenu->getAllChild("categories_multi", "cat_id", "cat_parent_id", 0, "lang_id = " . $lang_id, "cat_id,cat_name,cat_type,cat_has_child", "cat_type ASC,cat_order ASC,cat_name ASC", "cat_has_child", 0);
unset($classMenu);

//Khai báo biến khi thêm mới
$redirect			= getValue("redirect", "str", "GET", base64_encode("listing.php"));
$after_save_data	= getValue("after_save_data", "str", "POST", $redirect);
$add					= base64_encode("add.php");
$listing				= $redirect;
$gs_title			= $module_name . " | Sửa đổi";
$gs_action			= getURL();
$gs_redirect		= $after_save_data;
$gs_redirect		= base64_decode($gs_redirect);
$gs_errorMsg		= "";

//Get data edit
$record_id			= getValue("record_id");
$record_id			= getValue("record_id", "int", "POST", $record_id);
$db_edit				= new db_query("SELECT * FROM " . $gs_table . " WHERE " . $id_field . " = " . $record_id . " AND lang_id = " . $lang_id);
if(mysql_num_rows($db_edit->result) == 0){
	//Redirect if can not find data
	redirect($gs_error);
}
$edit					= mysql_fetch_array($db_edit->result);
unset($db_edit);

//Lấy dữ liệu đề giữ nguyên trạng thái khi submit error
$ban_name			= getValue("ban_name", "str", "POST", $edit["ban_name"]);
$ban_format			= getValue("ban_format", "int", "POST", $edit["ban_format"]);
$ban_width			= getValue("ban_width", "int", "POST", $edit["ban_width"]);
$ban_height			= getValue("ban_height", "int", "POST", $edit["ban_height"]);
$ban_html			= getValue("ban_html", "str", "POST", $edit["ban_html"]);
$ban_type			= getValue("ban_type", "int", "POST", $edit["ban_type"]);
$ban_link			= getValue("ban_link", "str", "POST", $edit["ban_link"]);
$ban_target			= getValue("ban_target", "str", "POST", $edit["ban_target"]);
$ban_order			= getValue("ban_order", "dbl", "POST", $edit["ban_order"]);
//$ban_hits			= getValue("ban_hits", "int", "POST", $edit["ban_hits"]);
$ban_str_start_date		= getValue("ban_str_start_date", "str", "POST", date("d/m/Y", $edit["ban_start_date"]));
$ban_str_start_time		= getValue("ban_str_start_time", "str", "POST", date("H:i:s", $edit["ban_start_date"]));
$ban_start_date			= convertDateTime($ban_str_start_date, $ban_str_start_time);
$ban_str_end_date		= getValue("ban_str_end_date", "str", "POST", date("d/m/Y", $edit["ban_end_date"]));
$ban_str_end_time		= getValue("ban_str_end_time", "str", "POST", date("H:i:s", $edit["ban_end_date"]));
if ($ban_str_end_date != "" && $ban_str_end_date != '01/01/1970')
{
	$ban_end_date	= convertDateTime($ban_str_end_date, $ban_str_end_time);
}
if ($ban_str_end_date == '01/01/1970')
{
	$ban_str_end_date = '';
	$ban_str_end_time = '';
}

// Category truy cập
$ban_category		= $edit["ban_category"];
if(isset($_POST["action"])){
	$ban_category	= "";
	$cat_array				= getValue("cat_array", "arr", "POST", "");
	if(is_array($cat_array)){
		foreach($cat_array as $key => $value){
			$ban_category .= "[" . intval($value) . "]";
		}
	}
}

$ban_active			= getValue("ban_active", "int", "POST", $edit["ban_active"]);

//Get action variable for add new data
$action				= getValue("action", "str", "POST", "");
//Check $action for insert new data
if($action == "execute"){
	
	//Lấy dữ liệu kiểu checkbox
	$ban_active		= getValue("ban_active", "int", "POST", 0);
	
	/*
	Call class form:
	1). Ten truong
	2). Ten form
	3). Kieu du lieu , 0 : string , 1 : kieu int, 2 : kieu email, 3 : kieu double, 4 : kieu hash password
	4). Noi luu giu data  0 : post, 1 : variable
	5). Gia tri mac dinh, neu require thi phai lon hon hoac bang default
	6). Du lieu nay co can thiet hay khong
	7). Loi dua ra man hinh
	8). Chi co duy nhat trong database
	9). Loi dua ra man hinh neu co duplicate
	*/
	$myform = new generate_form();
	//Add table insert data
	$myform->addTable($gs_table);
	$myform->add("ban_name", "ban_name", 0, 1, " ", 1, "Bạn chưa nhập tên quảng cáo.", 0, "");
	$myform->add("ban_format", "ban_format", 1, 1, 1, 1, "Bạn chưa chọn định dạng quảng cáo.", 0, "");
	$myform->add("ban_width", "ban_width", 1, 1, 0, 0, "", 0, "");
	$myform->add("ban_height", "ban_height", 1, 1, 0, 0, "", 0, "");
	$myform->add("ban_html", "ban_html", 0, 1, "", 0, "", 0, "");
	$myform->add("ban_type", "ban_type", 1, 1, 1, 1, "Bạn chưa chọn loại quảng cáo.", 0, "");
	$myform->add("ban_link", "ban_link", 0, 1, "", 0, "", 0, "");
	$myform->add("ban_target", "ban_target", 0, 1, "", 0, "", 0, "");
	$myform->add("ban_order", "ban_order", 3, 1, 0, 1, "Thứ tự phải lớn hơn hoặc bằng 0.", 0, "");
	//$myform->add("ban_hits", "ban_hits", 1, 1, 0, 0, "", 0, "");
	$myform->add("ban_start_date", "ban_start_date", 1, 1, 0, 0, "", 0, "");
	$myform->add("ban_end_date", "ban_end_date", 1, 1, 0, 0, "", 0, "");
	$myform->add("ban_category", "ban_category", 0, 1, "", 0, "", 0, "");
	$myform->add("ban_active", "ban_active", 1, 1, 0, 0, "", 0, "");

	//Check form data
	$gs_errorMsg .= $myform->checkdata();
	
	//Get $filename
	$filename		= "";
	if($gs_errorMsg == ""){
		$upload		= new upload($gs_fieldupload, $gs_filepath, $gs_extension, $gs_filesize, $gs_insert_logo);
		$filename	= $upload->file_name;
		$gs_errorMsg .= $upload->warning_error;
	}
	
	if($gs_errorMsg == ""){
		
		if($filename != ""){
			$$gs_fieldupload = $filename;
			//Kiểm tra xem nếu có ảnh cũ thì xóa đi
			if($edit[$gs_fieldupload] != "") $upload->delete_file($gs_filepath, $edit[$gs_fieldupload]);
			//Upload ảnh mới
			$myform->add($gs_fieldupload, $gs_fieldupload, 0, 1, "", 0, "", 0, "");
			//Nếu không phải file .swf thì mới resize
			if(getExtension($filename) != "swf") $upload->resize_image($gs_filepath, $filename, $width_small_image, $height_small_image, "small_");
		}//End if($filename != "")
		
		//Insert to database
		$myform->removeHTML(0);
		$db_update = new db_execute($myform->generate_update_SQL($id_field, $record_id));
		unset($db_update);
		
		//Redirect after insert complate
		redirect($gs_redirect);
		
	}//End if($gs_errorMsg == "")
	unset($myform);
	
}//End if($action == "insert")
?>
<html>
<head>
<title><?=$gs_title?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css" media="all">@import "../css/GSportal.css";</style>
<script language="javascript" src="../js/library.js"></script>
<script language="javascript" src="../js/multiselect.js"></script>
</head>
<body onLoad="changeBannerFormat(<?=$edit['ban_format']?>)">
<script type="text/javascript">
function changeBannerFormat(iFormat){
	iFormat = parseInt(iFormat);
	if(iFormat > 3 || iFormat < 1) iFormat = 1;
	
	if(iFormat == 1){
		document.getElementById("banner_image").style.display	= "table-row";
		document.getElementById("banner_size").style.display	= "none";
		document.getElementById("banner_html").style.display	= "none";
	}
	
	if(iFormat == 2){
		document.getElementById("banner_image").style.display	= "table-row";
		document.getElementById("banner_size").style.display	= "table-row";
		document.getElementById("banner_html").style.display	= "none";
	}
	
	if(iFormat == 3){
		document.getElementById("banner_image").style.display	= "none";
		document.getElementById("banner_size").style.display	= "none";
		document.getElementById("banner_html").style.display	= "table-row";
	}
}
</script>
<div class="bg_title_content">
    <div class="content_title" style="float:left"><?=$gs_title?></div>
    <div class="content_title" style="float:right"><a title="Quay về danh sách" href="<?=base64_decode($listing)?>"><img align="absmiddle" border="0" hspace="5" src="<?=$gs_imagepath?>list.gif" />Danh sách</a></div>
</div>
<div align="center" class="content">
<?
$onChange	= "changeBannerFormat(this.value)";
$form = new form();
$form->create_form("edit", $gs_action, "post", "multipart/form-data", 'onSubmit="selectAll(\'cat_array\')"');
$form->create_table();
?>
<?=$form->text_note('Những ô có dấu sao (<font class="form_asterisk">*</font>) là bắt buộc phải nhập.')?>
<?=$form->errorMsg($gs_errorMsg)?>
<?=$form->text("Tên banner", "ban_name", "ban_name", $ban_name, "Tên banner", 1, 250, "", 255, "", "", "")?>
<?=$form->select("Định dạng Banner", "ban_format", "ban_format", $arrFormat, $ban_format, "Định dạng Banner", 1, "", 0, 0, 'onChange="' . $onChange . '"', "")?>
<tr id="banner_image">
	<td class="form_name">Ảnh banner :</td>
	<td class="form_text">
		<input type="file" size="32" name="ban_picture" id="ban_picture" title="Ảnh banner" class="form_control" />
		(Dung lượng tối đa <font color="#ff0000"><?=$gs_filesize?> Kb</font>)
	</td>
</tr>
<tr id="banner_size" style="display:none">
	<td class="form_name">Kích thước :</td>
	<td class="form_text">
		<input type="text" maxlength="11" style="width: 50px;" value="<?=$ban_width?>" name="ban_width" id="ban_width" title="Chiều rộng" autocomplete="off" class="form_control" /> x <input type="text" maxlength="11" style="width: 50px;" value="<?=$ban_height?>" name="ban_height" id="ban_height" title="Chiều cao" autocomplete="off" class="form_control" />
	</td>
</tr>
<tr id="banner_html" style="display: none;">
	<td class="form_name">Nội dung HTML :</td>
	<td colspan="2">
		<script type="text/javascript" src="/js/tiny_mce/tiny_mce.js"></script>
        <script type="text/javascript">
				tinyMCE.init({
						// General options
						mode : "exact",
						elements : "ban_html",
						width : "300",
						height : "200",
						theme : "advanced",
						/* Dùng domain */
						remove_script_host : false,
						convert_urls : true,
						/* Dùng đường dẫn tuyệt đối */
						relative_urls : false,
						verify_html : true,
						cleanup : true,
						language : "vi",
						plugins : "emotions, table, paste, cleanupstyles",
						valid_elements : "a,b,blockquote,br,center,del,div,em,font,h2,h3,h4,i,img,ins,li,hr,ol,p,pre,s,span,strong,strike,sub,sup,table,tbody,td,th,tr,u,ul",
extended_valid_elements : "a[href|name|rel|style|target|title],b[style],blockquote[style|title],br[clear|title],center[],del[style|title],div[align|style|title|class],em[style|title],font[color|face|size|style|title],h2[],h3[],h4[],i[style|title],img[align|alt|border|height|hspace|idata|src|style|title|vspace|width],ins[style|title],li[style|title|type],hr[align|noshade|size|style|title],ol[style|title|type],p[align|style|title],pre[style|title],s[style|title],span[style|title|class],strong[style],strike[style|title],sub[style|title],sup[style|title],table[align|bgcolor|border|bordercolor|cellpadding|cellspacing|height|style|title|width],tbody[],td[align|bgcolor|colspan|height|nowrap|rowspan|style|title|valign|width],th[align|bgcolor|colspan|height|nowrap|rowspan|style|title|valign|width],tr[align|bgcolor|height|nowrap|style|title|valign],u[style|title],ul[style|title|type],",
invalid_styles : "behavior,background-image,background,list-style-image,expression,/*,*/",
				
						/* Theme options */
						theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,forecolor,backcolor",
						theme_advanced_buttons2 : "image,pasteword,link,unlink,emotions,|,table,tablecontrols,code",
						theme_advanced_buttons3 : "",
						theme_advanced_buttons4 : "",
						theme_advanced_toolbar_location : "top",
						theme_advanced_toolbar_align : "left",
						theme_advanced_statusbar_location : "bottom",
						theme_advanced_resizing : true,
						theme_advanced_resizing_use_cookie : false,
						theme_advanced_path : false, 
						/* Example content CSS (should be your site CSS) */
						content_css : "/css/tinyMCE.css"
				});
				
			</script>
        <textarea id="ban_html" name="ban_html" rows="20" cols="80" style="width: 300px"><?=$ban_html?></textarea>		
	</td>
</tr>
<?=$form->text("Liên kết", "ban_link", "ban_link", $ban_link, "Liên kết", 0, 250, "", 255, "", "", "")?>
<?=$form->select("Mở ra", "ban_target", "ban_target", $arrTarget, $ban_target, "Mở ra", 0, "", 1, 0, "", "")?>
<?=$form->select("Vị trí", "ban_type", "ban_type", $arrType, $ban_type, "Vị trí", 0, "", 1, 0, "", "")?>
<?=$form->text("Thứ tự", "ban_order", "ban_order", $ban_order, "Thứ tự", 2, 50, "", 255, "", "", "")?>
<? //$form->text("Lượt xem", "ban_hits", "ban_hits", $ban_hits, "Lượt xem", 2, 50, "", 255, "", "", "")?>
<?=$form->text("Ngày bắt đầu", "ban_str_start_date" . $form->ec . "ban_str_start_time", "ban_str_start_date" . $form->ec . "ban_str_start_time", $ban_str_start_date . $form->ec . $ban_str_start_time, "Ngày (dd/mm/yyyy)" . $form->ec . "Giờ (hh:mm:ss)", 0, 70 . $form->ec . 70, $form->ec, 10 . $form->ec . 10, " - ", $form->ec, "&nbsp; <i>(Ví dụ: dd/mm/yyyy - hh:mm:ss)</i>");?>
<?=$form->text("Ngày kết thúc", "ban_str_end_date" . $form->ec . "ban_str_end_time", "ban_str_end_date" . $form->ec . "ban_str_end_time", $ban_str_end_date . $form->ec . $ban_str_end_time, "Ngày (dd/mm/yyyy)" . $form->ec . "Giờ (hh:mm:ss)", 0, 70 . $form->ec . 70, $form->ec, 10 . $form->ec . 10, " - ", $form->ec, "&nbsp; <i>(Ví dụ: dd/mm/yyyy - hh:mm:ss)</i>");?>
<?
$size	= 10;
?>
<tr>
	<td class="form_name">Category :</td>
	<td class="form_text">
		<table cellpadding="0" cellspacing="0">
			<tr>
				<td style="padding:0px">
					<select class="form_control" id="cat_list" name="cat_list[]" multiple="multiple" size="<?=$size?>" style="width:200px">
					<?
					$cat_type = "";
					for($i=0; $i<count($listAll); $i++){
					?>
						<?
						if(strpos($ban_category, "[" . $listAll[$i]["cat_id"] . "]") === false){
						?>
							<?
							if($cat_type != $listAll[$i]["cat_type"]){
								$cat_type = $listAll[$i]["cat_type"];
							?>
								<optgroup label="<?=$arrCategoryType[$listAll[$i]["cat_type"]]?>"></optgroup>
							<?
							}
							?>
							<option value="<?=$listAll[$i]["cat_id"]?>">
								&nbsp; 
								<?
								for($j=-1; $j<$listAll[$i]["level"]; $j++) echo " |--";
								echo " " . $listAll[$i]["cat_name"];
								?>
							</option>
						<?
						}// End if(strpos($ban_category, "[" . $listAll[$i]["cat_id"] . "]") === false)
						?>
					<?
					}// End for($i=0; $i<count($listAll); $i++)
					?>
					</select>
				</td>
				<td style="padding:0px 12px 0px 12px; vertical-align:middle">
					<div style="margin-bottom:5px"><input type="button" class="form_button_arrow" value=">>" onClick="appendOption('cat_list','cat_array')" /></div>
					<div><input type="button" class="form_button_arrow" value="<<" onClick="appendOption('cat_array','cat_list')" /></div>
				</td>
				<td style="padding:0px">
					<select class="form_control" id="cat_array" name="cat_array[]" multiple="multiple" size="<?=$size?>" style="width:200px">
					<?
					$cat_type = "";
					for($i=0; $i<count($listAll); $i++){
					?>
						<?
						if(strpos($ban_category, "[" . $listAll[$i]["cat_id"] . "]") !== false){
						?>
							<?
							if($cat_type != $listAll[$i]["cat_type"]){
								$cat_type = $listAll[$i]["cat_type"];
							?>
								<optgroup label="<?=$arrCategoryType[$listAll[$i]["cat_type"]]?>"></optgroup>
							<?
							}
							?>
							<option value="<?=$listAll[$i]["cat_id"]?>">
								&nbsp; 
								<?
								for($j=-1; $j<$listAll[$i]["level"]; $j++) echo " |--";
								echo " " . $listAll[$i]["cat_name"];
								?>
							</option>
						<?
						}// End if(strpos($ban_category, "[" . $listAll[$i]["cat_id"] . "]") === false)
						?>
					<?
					}// End for($i=0; $i<count($listAll); $i++)
					?>
					</select>
				</td>
			</tr>
		</table>
	</td>
</tr>
<?=$form->checkbox("Kích hoạt", "ban_active", "ban_active", 1, $ban_active, "", 0, "", "")?>
<?=$form->radio("Sau khi lưu dữ liệu", "add_new" . $form->ec . "return_listing", "after_save_data", $add . $form->ec . $listing, $after_save_data, "Thêm mới" . $form->ec . "Quay về danh sách", 0, $form->ec, "");?>
<?=$form->button("submit" . $form->ec . "reset", "submit" . $form->ec . "reset", "submit" . $form->ec . "reset", "Cập nhật" . $form->ec . "Làm lại", "Cập nhật" . $form->ec . "Làm lại", 'style="background:url(' . $gs_imagepath . 'button_1.gif) no-repeat"' . $form->ec . 'style="background:url(' . $gs_imagepath . 'button_2.gif)"', "");?>
<?=$form->hidden("action", "action", "execute", "");?>
<?
$form->close_table();
$form->close_form();
unset($form);
?>
</div>
</body>
</html>
<script language="javascript">ButtonLeftFrame();</script>